package com.sky.jtail.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.apache.tomcat.util.codec.binary.Base64;


public class AesTools {


    //info:需要加密的明文
    public static String encrypt(String info,String secretKey) {
        byte[] key = new byte[0];
        try {
            key =  Base64.decodeBase64(secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DES des = SecureUtil.des(key);
        String encrypt = des.encryptHex(info);
        return encrypt;
    }


    //encrypt:需要解密的密文
    public static String decode(String encrypt,String secretKey) {
        byte[] key = new byte[0];
        try {
            key =  Base64.decodeBase64(secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DES des = SecureUtil.des(key);
        return des.decryptStr(encrypt);
    }

    public static void main1(String[] args) {
        //生成密钥，并转为字符串，可以储存起来，解密时可直接使用
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
        String secretKey =  Base64.encodeBase64String(key);

        System.out.println("加密密码："+secretKey);

        String content  = "这是重要的信息";

        //加密
        String jiamihou = encrypt(content,secretKey);
        //解密
        String yuanwen = decode(jiamihou,secretKey);

        System.out.println("加密后： "+jiamihou);
        System.out.println("原文： "+yuanwen);



    }
}
